package ups.MapParse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class LetterPattern {
	public static int 	PatternHeight = 9;
	
	public boolean[][] 	_bitmap;
	public int 			_width;
	public int			_height;
	private String 		_name;
	private String		_source;
	private int 		_offset;
	private Pixel		_color;
	
	public LetterPattern( int x, int y, int width, int height, BufferedImage image, boolean[][] bitmap)
	{
		initialize( x, y, width, height, image, bitmap );
	}
	
	public LetterPattern( int x, int y, int width, int height, BufferedImage image)
	{
		initialize( x, y, width, height, image, null );
	}
	
	public LetterPattern( int x, int y, int width, int height, int[] imageArray, int imageStride, boolean[][] bitmap, PixelCache cache ) {
		initializeDirect( x, y, width, height, imageArray, imageStride, bitmap, cache);
	}
	
	public LetterPattern( String file ) {
		try {
			BufferedImage image = ImageIO.read(new File( file ));
			initialize( 0,0, image.getWidth(), image.getHeight(), image, null );
		} catch( IOException e )
		{
			System.out.println("Couldn't make a pattern: " + e.toString());
			System.out.println( "File was " + file );
		}
	}
	
	public LetterPattern( String file, int color ){
		try {
			BufferedImage image = ImageIO.read(new File( file ));
			initialize( 0,0, image.getWidth(), image.getHeight(), image, null );
		} catch( IOException e )
		{
			System.out.println("Couldn't make a pattern: " + e.toString());
			System.out.println( "File was " + file );
		}
	}


	public static int initCount = 0;
	private final void initializeDirect( int x, int y, int width, int height, int[] imageArray, int imageStride, boolean[][] bitmap, PixelCache cache )
	{
		if( bitmap == null ) {
			initCount++;
			_bitmap = new boolean[width][height];
		} else {
			_bitmap = bitmap;
		}
		_width = width;
		_height = height;
		
		int i, j;
		for( j = y; j < y + height; j++ ) {
			for( i = x; i < x + width; i++)
			{
				Pixel p = cache.getPixel(imageArray[ j * imageStride + i] );
				if( p.equals( Pixel.redPixel ) || p.equals( Pixel.bluePixel )) {
					_bitmap[i-x][j-y] = true;
					_color = p;
				} else {
					_bitmap[i-x][j-y] = false;
				}
				
			}
		}
	}

	private final void initialize( int x, int y, int width, int height, BufferedImage image, boolean[][] bitmap )
	{
		if( bitmap == null ) {
			initCount++;
			_bitmap = new boolean[width][height];
		} else {
			_bitmap = bitmap;
		}
		_width = width;
		_height = height;
		
		int i, j;
		for( j = y; j < y + height; j++ ) {
			for( i = x; i < x + width; i++)
			{
				Pixel p = new Pixel( image.getRGB(i, j));

				if( p.equals( Pixel.redPixel ) || p.equals( Pixel.bluePixel )) {
					_bitmap[i-x][j-y] = true;
				} else {
					_bitmap[i-x][j-y] = false;
				}
				
			}
		}
	}
	
	/* Compare */
	public boolean compare( LetterPattern otherPattern )
	{
		if( this.getHeight() != otherPattern.getHeight() || this.getWidth() != otherPattern.getWidth() ) {
			return false;
		}
		int x, y;
		for( x = 0; x < this.getWidth(); x++ ) {
			for( y = 0; y < this.getHeight(); y++ ) {
				if( _bitmap[x][y] != otherPattern._bitmap[x][y] ) {
					return false;
				}
			}
		}
		return true;
	}
	
	/* Pretty Printing */
	public void prettyPrint() {
		System.out.println("Name: " + this.getName() + " (" + this.getWidth() + ", " + this.getHeight() + ")");
		int x,y;
		for( y  = 0; y < this.getHeight(); y++ )
		{
			for( x = 0; x < this.getWidth(); x++ ) {
				if( _bitmap[x][y] ) {
					System.out.print( "*" );
				}
				else {
					System.out.print("0");
				}
			}
			System.out.println();
		}
	}
	
	protected void prettyPrintCompare( LetterPattern otherPattern ) {
		int x, y;
		System.out.printf( "Dimensions: %dx%d\n", this.getWidth(), this.getHeight());
		for( y = 0 ; y < this.getHeight() ; y++ ) {
			for( x  = 0 ; x < this.getWidth(); x++ ) {
				if( this._bitmap[x][y] ) {
					System.out.print( "#");
				} else {
					System.out.print( " ");	
				}
			}
			System.out.print( " | " );
			for( x  = 0 ; x < otherPattern.getWidth(); x++ ) {
				if( otherPattern._bitmap[x][y] ) {
					System.out.print( "#");
				} else {
					System.out.print( " ");	
				}				
			}
			System.out.println();
		}
		
		System.out.println();
	}
	
	
	/* Accessors */
	public void setSourceFileName( String sourceFile ) {
		_source = sourceFile;
	}
	
	public String getSourceFileName() {
		return _source;
	}
	
	public void setName( String name )
	{
		_name = name;
	}
	
	public String getName()
	{
		return _name;
	}
	
	public int getHeight() {
		return _height;
	}
	
	public int getWidth() {
		return _width;
	}
	
	public int getOffset() {
		return _offset;
	}
	
	public void setOffset(int offset) {
		_offset = offset;
	}
	
	public Pixel getColor() {
		return _color;
	}
	
	public void setColor( Pixel color ) {
		_color = color;
	}
	
	public String toString() {
		return this.getName() + " (" + this.getWidth() + ")";
	}

}
